﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HtmlReportHelper.cs" company="">
//   
// </copyright>
// <summary>
//   The html report helper.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UITest.CUIT.HtmlReport
{
    using System;
    using System.Diagnostics;

    using Microsoft.VisualStudio.TestTools.UITesting;

    using UITest.CUIT.CuitControls.Extension;

    /// <summary>
    ///     The html report helper.
    /// </summary>
    public class HtmlReportHelper
    {
        #region Fields

        private bool generateHtmlReport;

        private bool saveErrorScreenCaptureToFile;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlReportHelper"/> class.
        /// </summary>
        /// <param name="isGenerateHtmlReport">
        /// The is generate html report.
        /// </param>
        public HtmlReportHelper(bool isGenerateHtmlReport)
        {
            this.GenerateHtmlReport = isGenerateHtmlReport;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets or sets a value indicating whether generate html report.
        /// </summary>
        public bool GenerateHtmlReport
        {
            get
            {
                this.generateHtmlReport = HtmlReport.GenerateHtmlReport;
                return this.generateHtmlReport;
            }

            set
            {
                this.generateHtmlReport = value;
                HtmlReport.GenerateHtmlReport = this.generateHtmlReport;
            }
        }

        /// <summary>
        ///     Gets or sets the report full path name.
        /// </summary>
        public string ReportFullPathName
        {
            get
            {
                return HtmlReport.ReportFullPathName;
            }

            set
            {
                HtmlReport.ReportFullPathName = value;
            }
        }

        /// <summary>
        ///     Gets or sets a value indicating whether save error screen capture to file.
        /// </summary>
        public bool SaveErrorScreenCaptureToFile
        {
            get
            {
                this.saveErrorScreenCaptureToFile = HtmlReport.SaveErrorScreenCaptureToFile;
                return this.saveErrorScreenCaptureToFile;
            }

            set
            {
                this.saveErrorScreenCaptureToFile = value;
                HtmlReport.SaveErrorScreenCaptureToFile = this.saveErrorScreenCaptureToFile;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     The html report closure.
        /// </summary>
        public void HtmlReportClosure()
        {
            Playback.PlaybackError -= this.Playback_PlaybackError;
            HtmlReport.CloseReportHTML();
        }

        /// <summary>
        /// The html report enhanced append.
        /// </summary>
        /// <param name="text">
        /// The text.
        /// </param>
        /// <param name="htmltag">
        /// The htmltag.
        /// </param>
        /// <param name="htmlClass">
        /// The html class.
        /// </param>
        public void HtmlReportEnhancedAppend(string text, string htmltag = "DIV", string htmlClass = "")
        {
            HtmlReport.EnhancedAppendHTML(text, htmltag, htmlClass);
        }

        /// <summary>
        /// The html report initialize.
        /// </summary>
        /// <param name="reportPath">
        /// The report path.
        /// </param>
        /// <param name="stackFrame">
        /// The stack Frame.
        /// </param>
        public void HtmlReportInitialize(string reportPath, int stackFrame = 2)
        {
            Playback.PlaybackError += this.Playback_PlaybackError;
            HtmlReport.ReportName = "HtmlReport_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
            HtmlReport.ReportPath = reportPath;
            var frame = new StackFrame(stackFrame);
            HtmlReport.ReportHeader = frame.GetMethod().Name;
            HtmlReport.CreateReportHTML();
        }

        /// <summary>
        /// The html report simple append.
        /// </summary>
        /// <param name="text">
        /// The text.
        /// </param>
        public void HtmlReportSimpleAppend(string text)
        {
            HtmlReport.SimpleAppendReportHTML(text);
        }

        /// <summary>
        /// The set embedded css path.
        /// </summary>
        /// <param name="embededCssName">
        /// The embeded css name.
        /// </param>
        public void SetEmbeddedCssPath(string embededCssName)
        {
            HtmlReport.EmbededCss = embededCssName;
        }

        /// <summary>
        /// The set external css path.
        /// </summary>
        /// <param name="externalCssPath">
        /// The external css path.
        /// </param>
        public void SetExternalCssPath(string externalCssPath)
        {
            HtmlReport.ExternalCss = externalCssPath;
        }

        /// <summary>
        /// The set external css path.
        /// </summary>
        /// <param name="externalCssPathList">
        /// The external css path list.
        /// </param>
        public void SetExternalCssPath(string[] externalCssPathList)
        {
            HtmlReport.ExternalCssList = externalCssPathList;
        }

        /// <summary>
        /// The set screen capture level.
        /// </summary>
        /// <param name="screenCaptureLevel">
        /// The screen capture level.
        ///     <see cref="ScreenCaptureLevel"/> class for more Detail
        /// </param>
        public void SetScreenCaptureLevel(int screenCaptureLevel)
        {
            HtmlReport.ScreenCaptureLevel = screenCaptureLevel;
        }

        #endregion

        #region Methods

        private void Playback_PlaybackError(object sender, PlaybackErrorEventArgs e)
        {
            HtmlReport.AppendReportErrorHTML(e.Error.ExtensionFullMessage());
        }

        #endregion
    }
}